package cn.sdormitory.job.dormitory;

import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.attendance.dao.HolidayConfigDao;
import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.sms.TxSmsTemplate;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: DormitoryExceptionAttenceNoticeJob
 * @author: Alex
 * @date: 2024/9/13
 **/
@Slf4j
public class DormitoryExceptionAttenceNoticeJob  extends QuartzJobBean {
	
	@Autowired
	SysUserDao sysUserDao;
	
	@Autowired
	SdAttenceDao sdAttenceDao;
	
	@Autowired
	private TxSmsTemplate txSmsTemplate;
	
	@Autowired
	private HolidayConfigDao holidayConfigDao;
	
	@Autowired
	private SysDeptDao sysDeptDao;
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		log.info("开始发送晚寝未打卡的名单短信给班主任。。。。");
		Date date = new Date();
        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");
		
//		String accessDate = "2024-09-12";
		//获取所有假期
		List<HolidayConfig> holiday = holidayConfigDao.getHolidayByDate(accessDate);
		
		//查询所有班主任老师
		List<SysUser> classTeacher = sysUserDao.getUserListByRole("classTeacher");
		
		Map<String, String[]> modelMsg = new HashMap<>();
		for(SysUser user : classTeacher){
			//判断当前班主任老师所在部门是否放假
			boolean isHoliday = false;
			if(holiday != null && holiday.size() > 0){
				for(HolidayConfig item : holiday){
					//判断当前放假部门是否有子部门
					List<Long> deptChildId = sysDeptDao.getAllChildByParent(item.getDeptId());
					boolean flag = deptChildId.stream().anyMatch(temp -> temp.equals(user.getDeptId()));
					if(flag && item.getIsHoliday().equals("1") && !accessDate.equals(item.getHolidayEnd())){
						isHoliday = true;
						break;
					}
				}
			}
			//放月假
			if(isHoliday){
				continue;
			}
			
			//获取宿舍未归寝学生名单
			List<BStudent> studentList = sdAttenceDao.getClassUnderJurisdictionOfStuByStatus(user.getId(), accessDate, "2");
			if(CollectionUtil.isEmpty(studentList)){
				continue;
			}
			
			//拼凑短信内容
			StringBuilder smsContent = new StringBuilder();
			for(BStudent student : studentList){
				smsContent.append(student.getStudentName()).append("，");
			}
			
			String[] params = {accessDate,smsContent.substring(0, smsContent.length() - 1) + "。"};
			modelMsg.put(user.getPhone(), params);
//			if(user.getPhone() != null){
//				txSmsTemplate.sendMesModel(user.getPhone(), params, 5);
////                txSmsTemplate.sendMesModel("18152606788", params, 5);
//			}
		}
		
		for (Map.Entry<String, String[]> stringEntry : modelMsg.entrySet()) {
			txSmsTemplate.sendMesModel(stringEntry.getKey(), stringEntry.getValue(), 5);
		}
	}
}
